using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Life
{
    /// <summary>
    /// Represents a touchable button.
    /// </summary>
    class Button : Control
    {
        /// <summary>
        /// The text displayed in the button.
        /// </summary>
        public string Text = "Button";

        /// <summary>
        /// The thickness of the border drawn for the button.
        /// </summary>
        public int BorderThickness = 0;

        /// <summary>
        /// The color of the button border.
        /// </summary>
        //public Color BorderColor = new Color(200, 200, 200);
        public Color BorderColor = Color.Black * .9f;

        /// <summary>
        /// The color of the button background.
        /// </summary>
        //public Color FillColor = new Color(100, 100, 100) * .85f;
        public Color FillColor = Color.DarkGreen * .99f;

        /// <summary>
        /// The color of the text.
        /// </summary>
        public Color TextColor = new Color(192, 192, 192) * .75f;

        /// <summary>
        /// The opacity of the button.
        /// </summary>
        public float Alpha = 0f;

        /// <summary>
        /// Creates a new Button.
        /// </summary>
        /// <param name="text">The text to display in the button.</param>
        public Button(string text, Vector2 size)
            : this(text, size, 0f)
        {
        }

        public Button(string text, Vector2 size, float alpha)
        {
            this.Text = text;
            this.Size = size;
            this.Alpha = alpha;


        }

        /// <summary>
        /// Draws the button
        /// </summary>
        /// <param name="screen">The screen drawing the button</param>
        public override void Draw(DrawContext context)
        {
            SpriteBatch spriteBatch = context.SpriteBatch;
            SpriteFont font = context.ButtonFont;
            Texture2D blank = context.BlankTexture;

            Vector2 absPos = Position + context.DrawOffset;
            
            // Compute the button's rectangle
            Rectangle r = new Rectangle(
                (int)absPos.X,
                (int)absPos.Y,
                (int)Size.X,
                (int)Size.Y);

            // Fill the button
            spriteBatch.Draw(blank, r, FillColor * Alpha);

            // Draw the border
            spriteBatch.Draw(
                blank, 
                new Rectangle(r.Left, r.Top, r.Width, BorderThickness),
                BorderColor * Alpha);
            spriteBatch.Draw(
                blank, 
                new Rectangle(r.Left, r.Top, BorderThickness, r.Height),
                BorderColor * Alpha);
            spriteBatch.Draw(
                blank, 
                new Rectangle(r.Right - BorderThickness, r.Top, BorderThickness, r.Height),
                BorderColor * Alpha);
            spriteBatch.Draw(
                blank, 
                new Rectangle(r.Left, r.Bottom - BorderThickness, r.Width, BorderThickness),
                BorderColor * Alpha);

            // Draw the text centered in the button
            Vector2 textSize = font.MeasureString(Text);
            Vector2 textPosition = new Vector2(r.Center.X, r.Center.Y) - textSize / 2f;
            textPosition.X = (int)textPosition.X;
            textPosition.Y = (int)textPosition.Y;
            spriteBatch.DrawString(font, Text, textPosition, TextColor * Alpha);

            base.Draw(context);
        }
    }
}
